<?php

declare(strict_types=1);
/**
 * 创建人 : Forpastime
 *
 * @link     http://hypercmf.com
 * @document https://hyperf.wiki
 * @contact  forpastime@qq.com
 * @git  https://gitee.com/hypercmf/hypercmf1.0.git
 */
namespace App\Controller;

use Hyperf\Contract\ConfigInterface;
use Hyperf\Contract\SessionInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\View\RenderInterface;

/**
 * @AutoController(prefix="/admin")
 *
 * Class BaseController 后台基类，主要继承AbstractController后， 添加一些常用方法
 */
class AdminBaseController extends AbstractController
{
    /**
     * //@Inject
     * @var SessionInterface
     */
//    protected $session;

    /**
     * @Inject
     * @var RenderInterface
     */
    protected $render;

    /**
     * @Inject
     * @var RequestInterface
     */
    protected $request;

    /**
     * @Inject
     * @var ConfigInterface
     */
    protected $config;

    public function __construct()
    {
//        $admin = $this->session->get('user');
//
//        if(empty($admin) || !isset($admin['name']) || empty($admin['name'])){
//            var_dump($admin);
//            return $this->fetch('admin/login/login');
//        }
    }

    /**
     * Redirect to a URL.
     * @param mixed $toUrl
     * @param mixed $status
     * @param mixed $schema
     */
    public function redirect($toUrl, $status = 302, $schema = 'http')
    {
        return $this->response->redirect($toUrl, $status, $schema);
    }

    /**
     * @param mixed $userId
     */
    public function checkAuth($userId)
    {
        if ($userId == 1) {
            return true;
        }
    }

    /**
     * 视图模板渲染.
     * @param string $template
     * @return \Psr\Http\Message\ResponseInterface
     */
    protected function fetch($template = '', array $vars = [])
    {
        if ($template != '') {
            $filePath = $this->config->get('view.config.view_path') . $template . $this->config->get('view.config.view_file_extension');
            if (! file_exists($filePath)) {
                return $template . $this->config->get('view.config.view_file_extension') . ' Template file not exist!';
            }
            return $this->render->render($template, $vars);
        }
        $filePath = $this->config->get('view.config.view_path') . $this->request->path() . $this->config->get('view.config.view_file_extension');
        if (! file_exists($filePath)) {
            return $this->request->path() . $this->config->get('view.config.view_file_extension') . ' Template file not exist!';
        }
        return $this->render->render($this->request->path(), $vars);
    }

    /**
     * 操作成功
     * @param mixed $msg 提示信息
     * @param mixed $data 返回的数据
     * @param array $header 发送的Header信息
     */
    protected function success($msg = '请求成功', $data = [], array $header = [])
    {
        $code = 1;
        if (is_array($msg)) {
            $code = $msg['code'];
            $msg = $msg['msg'];
        }
        $result = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];

        return $this->response->json($result);
    }

    /**
     * 操作错误.
     * @param mixed $msg 提示信息,若要指定错误码,可以传数组,格式为['code'=>您的错误码,'msg'=>'您的错误消息']
     * @param mixed $data 返回的数据
     * @param array $header 发送的Header信息
     */
    protected function error($msg = '', $data = '', array $header = [])
    {
        $code = 0;
        if (is_array($msg)) {
            $code = $msg['code'];
            $msg = $msg['msg'];
        }
        $result = [
            'code' => $code,
            'msg' => $msg,
            'data' => $data,
        ];
        return $this->response->json($result);
    }
}
